package com.game.logic.items.title;

import com.coment.cfg.luban.Tables;
import com.coment.cfg.luban.items.TitleCfg;
import com.coment.game.fight.AttributeManagerLogic;
import com.coment.game.fight.Attrs;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;

@Data
public class UserTitle {
    @Id
    private Integer id;

    /// 玩家id
    private Integer userInfoId;

    /// 称号
    private Integer title;

    public UserTitle(Integer userInfoId, Integer title) {
        this.id = Objects.hash(userInfoId, title);
        this.userInfoId = userInfoId;
        this.title = title;
    }


    // ----------------------------------------------  读取配置功能部分
    /// 称号属性
    public static final Map<Integer, Attrs> TITLE_ATTRS = new HashMap<>();
    static {
        Tables.reloadHandlers.add(loadingCfg());
    }

    private static Function<Integer, Boolean> loadingCfg() {
        return (id) -> {
            TITLE_ATTRS.clear();
            for (TitleCfg titleCfg : Tables.cfg.get_tbtitlecfg().get_dataList()) {
                Attrs attrs = AttributeManagerLogic.obtainCfgAttrs(titleCfg);
                TITLE_ATTRS.put(titleCfg.id, Optional.of(attrs).orElseGet(Attrs::new));
            }
            return true;
        };
    }
    // -----------------------------------------------
    @Transient
    public Attrs getAttrs() {
        return TITLE_ATTRS.get(title);
    }
}
